import 'package:flutter/material.dart';
import '../my_change_notifier.dart';
import '../provider_creator.dart';

typedef ConsumerWidgetBuilder<T extends MyChangeNotifier> = Widget Function(BuildContext context, T? viewModel);

//可以方便的获取MyChangeNotifier的具体类型
//author:liuhaichao
class MyConsumer<T extends MyChangeNotifier> extends StatelessWidget {
  const MyConsumer({
    Key? key,
    this.dependOn = true,
    required this.consumerWidgetBuilder,
  }) : super(key: key);

  final bool dependOn;
  final ConsumerWidgetBuilder<T> consumerWidgetBuilder;

  @override
  Widget build(BuildContext context) {
    return consumerWidgetBuilder(context, ProviderCreator.of<T>(context, dependOn: dependOn));
  }
}
